<!--
 * @Description: 加工方式
 * 
 * @Author: XiongYaoYuan<xiongyaoyuan>
 * @Date: 2023-04-24 14:30:47
 * @FilePath: /yuxiuproweb/src/views/process/way/index.vue
-->
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="加工方式名称" prop="processingMethodName">
        <el-input
          v-model="queryParams.processingMethodName"
          placeholder="请输入加工方式名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['md:method:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['md:method:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['md:method:remove']"
          >删除</el-button
        >
      </el-col>
      
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="methodList"
      @selection-change="handleSelectionChange"
      :max-height="maxHeight"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="加工方式名称"
        align="center"
        prop="processingMethodName"
        min-width="150"
      />
      <el-table-column
        label="销售别名"
        align="center"
        min-width="100"
        prop="alias"
      />
      <el-table-column
        label="用料类型"
        align="center"
        min-width="100"
        prop="field"
      >
        <template slot-scope="{ row }">
          {{ getTypeName(FIELDS, row.field) }}
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" min-width="100" prop="type">
        <template slot-scope="{ row }">
          {{ getTypeName(TYPES, row.type) }}
        </template>
      </el-table-column>
      <el-table-column
        label="出厂价"
        align="center"
        min-width="100"
        prop="factoryPrice"
      />
      <el-table-column
        label="经销价"
        align="center"
        min-width="100"
        prop="distributorPrice"
      />
      <el-table-column
        label="高定经销价"
        align="center"
        prop="hcDistributionPrice"
        min-width="100"
      />
      <el-table-column
        label="窗帘零售价"
        align="center"
        min-width="100"
        prop="curtainPrice"
      />
      <el-table-column
        label="高定零售价"
        align="center"
        min-width="100"
        prop="hcCurtainPrice"
      />
      <el-table-column label="是否启用" align="center" prop="enable">
        <template slot-scope="{ row }">
          {{ row.enable === "Y" ? "是" : "否" }}
        </template>
      </el-table-column>
      <el-table-column label="是否定型" align="center" prop="finalized">
        <template slot-scope="{ row }">
          {{ `${row.finalized}` === "0" ? "是" : "否" }}
        </template>
      </el-table-column>
      <el-table-column label="褶子尺寸" align="center" prop="tuckSize" />
      <el-table-column
        label="裁高机修正高度"
        align="center"
        prop="correctedAltitude"
        min-width="130"
      />
      <el-table-column
        label="每条侧边卷边损耗"
        min-width="130"
        align="center"
        prop="loss"
      />
      <el-table-column
        label="定高高度损耗"
        min-width="130"
        align="center"
        prop="heightLoss"
      />
      <el-table-column
        label="定宽高度损耗"
        min-width="130"
        align="center"
        prop="widthLoss"
      />
      <el-table-column
        label="工序维护/操作"
        align="center"
        min-width="130"
        fixed="right"
        prop="process"
      >
        <template slot-scope="{ row }">
          <!-- v-hasPermi="['md:model:edit']" -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleProcess(row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        min-width="130"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['md:method:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['md:method:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改加工方式对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="open"
      width="1000px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="加工方式名称" prop="processingMethodName">
              <el-input
                v-model="form.processingMethodName"
                placeholder="请输入加工方式名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="销售别名" prop="alias">
              <el-input v-model="form.alias" placeholder="请输入销售别名" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="用料类型" prop="field">
              <el-select
                v-model="form.field"
                placeholder="请选择用料类型"
                style="width: 100%"
              >
                <el-option
                  v-for="item in FIELDS"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型" prop="type">
              <el-select
                v-model="form.type"
                placeholder="请选择类型"
                style="width: 100%"
              >
                <el-option
                  v-for="item in TYPES"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="是否启用" prop="enable">
              <el-switch
                v-model="form.enable"
                active-value="Y"
                inactive-value="N"
              ></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否定型" prop="finalized">
              <el-switch
                v-model="form.finalized"
                :active-value="0"
                :inactive-value="1"
              ></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="褶子尺寸(米)" prop="tuckSize">
              <el-input-number
                v-model="form.tuckSize"
                placeholder="请输入褶子尺寸"
                clearable
                style="width: 100%"
                :min="0"
                :controls="false"
                :precision="2"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="裁高机修正高度(米)" prop="correctedAltitude">
              <el-input-number
                v-model="form.correctedAltitude"
                placeholder="请输入裁高机修正高度"
                clearable
                style="width: 100%"
                :min="0"
                :controls="false"
                :precision="2"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="每条侧边卷边损耗(米)" prop="loss">
              <el-input-number
                v-model="form.loss"
                placeholder="请输入每条侧边卷边损耗"
                clearable
                style="width: 100%"
                :min="0"
                :controls="false"
                :precision="2"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="定高高度损耗(米)" prop="heightLoss">
              <el-input-number
                v-model="form.heightLoss"
                placeholder="请输入定高高度损耗"
                clearable
                style="width: 100%"
                :min="0"
                :controls="false"
                :precision="2"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="定宽高度损耗(米)" prop="widthLoss">
              <el-input-number
                v-model="form.widthLoss"
                placeholder="请输入定宽高度损耗"
                clearable
                style="width: 100%"
                :min="0"
                :controls="false"
                :precision="2"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="出厂价" prop="factoryPrice">
              <el-input-number
                v-model="form.factoryPrice"
                placeholder="请输入出厂价"
                clearable
                style="width: 100%"
                :min="0"
                :controls="false"
                :precision="2"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="窗帘经销价" prop="distributorPrice">
              <el-input-number
                v-model="form.distributorPrice"
                placeholder="请输入窗帘经销价"
                clearable
                style="width: 100%"
                :min="0"
                :controls="false"
                :precision="2"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="高定经销价" prop="hcDistributionPrice">
              <el-input-number
                v-model="form.hcDistributionPrice"
                placeholder="请输入高定经销价"
                clearable
                style="width: 100%"
                :min="0"
                :controls="false"
                :precision="2"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="窗帘零售价" prop="curtainPrice">
              <el-input-number
                v-model="form.curtainPrice"
                placeholder="请输入窗帘零售价"
                clearable
                style="width: 100%"
                :min="0"
                :controls="false"
                :precision="2"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="高定零售价" prop="hcCurtainPrice">
              <el-input-number
                v-model="form.hcCurtainPrice"
                placeholder="请输入高定零售价"
                style="width: 100%"
                clearable
                :min="0"
                :controls="false"
                :precision="2"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="图片" prop="img">
          <image-upload v-model="form.img" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 工序维护 -->
    <process-drawer
      v-if="process.show"
      :show.sync="process.show"
      v-bind="process"
    ></process-drawer>
  </div>
</template>

<script>
import {
  listMethod,
  getMethod,
  delMethod,
  addMethod,
  updateMethod,
} from "@/api/process/way";
import { TYPES, FIELDS } from "./configure";
import ProcessDrawer from "@/views/process/vm/mods/process/index.vue";
import commonMix from "@/utils/mixins/common";

export default {
  name: "Way",
  mixins: [commonMix],
  components: {
    ProcessDrawer,
  },
  data() {
    return {
      TYPES,
      FIELDS,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 加工方式表格数据
      methodList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        processingMethodName: null,
        alias: null,
        field: null,
        type: null,
        factoryPrice: null,
        distributorPrice: null,
        hcDistributionPrice: null,
        curtainPrice: null,
        hcCurtainPrice: null,
        enable: null,
        finalized: null,
        tuckSize: null,
        correctedAltitude: null,
        loss: null,
        heightLoss: null,
        widthLoss: null,
        img: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        processingMethodName: [
          { required: true, message: "请输入加工方式名称", trigger: "change" },
        ],
        field: [
          { required: true, message: "请输入用料类型", trigger: "change" },
        ],
        type: [{ required: true, message: "请选择类型", trigger: "change" }],
        // enable: [{ required: true, message: "是否启用", trigger: "change" }],
        // finalized: [{ required: true, message: "是否定型", trigger: "change" }],
        tuckSize: [
          { required: true, message: "请输入褶子尺寸(米)", trigger: "change" },
        ],
        correctedAltitude: [
          {
            required: true,
            message: "请输入裁高机修正高度(米)",
            trigger: "change",
          },
        ],
        loss: [
          {
            required: true,
            message: "请输入每条侧边卷边损耗(米)",
            trigger: "change",
          },
        ],
        heightLoss: [
          {
            required: true,
            message: "请输入定高高度损耗(米)",
            trigger: "change",
          },
        ],
        widthLoss: [
          {
            required: true,
            message: "请输入定宽高度损耗(米)",
            trigger: "change",
          },
        ],
      },
      process: {
        show: false,
        productId: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询加工方式列表 */
    getList() {
      this.loading = true;
      listMethod(this.queryParams).then((response) => {
        this.methodList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        processingMethodId: null,
        processingMethodName: null,
        alias: null,
        field: null,
        type: null,
        factoryPrice: null,
        distributorPrice: null,
        hcDistributionPrice: null,
        curtainPrice: null,
        hcCurtainPrice: null,
        enable: null,
        finalized: null,
        tuckSize: null,
        correctedAltitude: null,
        loss: null,
        heightLoss: null,
        widthLoss: null,
        img: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.processingMethodId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加加工方式";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const processingMethodId = row.processingMethodId || this.ids;
      getMethod(processingMethodId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改加工方式";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          console.log(this.form);
          if (this.form.processingMethodId != null) {
            updateMethod(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMethod(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const processingMethodIds = row.processingMethodId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除加工方式编号为"' + processingMethodIds + '"的数据项？'
        )
        .then(function () {
          return delMethod(processingMethodIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    

    /** 工序维护 */
    handleProcess({ processingMethodId }) {
      this.process = {
        show: true,
        productId: processingMethodId,
        type: "1",
      };
    },

    //图片上传成功
    handleImgUplaoded(imgUrl) {
      console.log(imgUrl);
      //   this.form.sopUrl = imgUrl;
    },
    //图片移除
    handleImgRemoved(imgUrl) {
      console.log(imgUrl);
      //   this.form.sopUrl = null;
    },
  },
};
</script>
